<template>
  <textarea id="epanet-report"></textarea>
</template>

<script setup>
import { Project, Workspace } from "epanet-js";

// 异步读取文件
async function readTextFile(path, encoding = "utf-8") {
  const file = new URL(path, import.meta.url);
  const response = await fetch(file);
  const text = await response.text();
  return text;
}

readTextFile("./南沙模型-DN400.inp")
  .then((text) => {
    let net1 = text;

    // Initialise a new Workspace and Project object
    const ws = new Workspace();
    const model = new Project(ws);

    // Write a copy of the inp file to the virtual workspace
    ws.writeFile("net1.inp", net1);

    // Runs toolkit methods: EN_open, EN_solveH & EN_close
    model.open("net1.inp", "report.rpt", "out.bin");
    model.solveH();
    model.close();

    const reportFile = ws.readFile("report.rpt");

    document.getElementById("epanet-report").innerHTML = reportFile;
  })
  .catch((error) => {
    console.error("Error reading file:", error);
  });
</script>

<style scoped></style>
